package com.baoleme.service.impl;

import com.baoleme.dto.UpdateDefaultAddressDTO;
import com.baoleme.entity.AddressBook;
import com.baoleme.mapper.AddressBookMapper;
import com.baoleme.service.AddressBookService;
import com.baoleme.vo.AddressByUserIdVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2024-09-23
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {


    @Override
    public List<AddressBook> queryAllAddressByUserId(String userId) {
        LambdaQueryWrapper<AddressBook> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);

        List<AddressBook> list = this.list(wrapper);


        return list;
    }

    @Override
    public AddressBook querydefaultAddress(String userId) {
        LambdaQueryWrapper<AddressBook> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1)
                .eq(AddressBook::getUserId,userId);

        AddressBook addressBook = this.getOne(wrapper);

        return addressBook;
    }

    /*参数的数据验证没有*/
    @Override
    public boolean updatedefaultAddress(UpdateDefaultAddressDTO updateDefaultAddressDTO) {
        //查询条件
        LambdaQueryWrapper<AddressBook> wrapper=new LambdaQueryWrapper<>();
        System.out.println(updateDefaultAddressDTO);
        wrapper.eq(AddressBook::getUserId,updateDefaultAddressDTO.getUserId())
                .eq(AddressBook::getIsDefault,1);
        //查询得到一条默认地址
        AddressBook addressBook1 = this.getOne(wrapper);
        if(addressBook1!=null) {
            addressBook1.setIsDefault(0);
            this.updateById(addressBook1);
        }
        AddressBook addressBook = this.getById(updateDefaultAddressDTO.getId());
        addressBook.setIsDefault(1);
        return this.updateById(addressBook);
    }
}
